<template>
  <el-container>
    <el-header>
      <div class="userImg">
        <img src="../assets/image/avatar.jpg" alt />
      </div>
      <p>后台管理系统</p>
      <el-button type="primary" @click="out">退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左侧菜单栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 列表内容 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          router
        >
          <!-- 以及列表 -->
          <el-submenu :index="'/'+item.path" :key="item.id" v-for="item in menuList">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <!-- 二级列表 -->
            <el-menu-item-group :key="items.id" v-for="items in item.children">
              <el-menu-item :index="'/'+items.path" @click="saveNavState('/'+items.path)">
                <i class="el-icon-menu"></i>
                {{items.authName}}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { homeList } from "@/api";
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      //点击折叠
      isCollapse: false,
      activePath: "users"
    };
  },
  methods: {
    //退出登录
    out() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/");
    },
    // 点击折叠列表
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      this.activePath = activePath;
      window.sessionStorage.setItem("activePath", activePath);
    },
    // 获取页面消息
    async getMenuList() {
      var { data: res } = await homeList();
      this.menuList = res.data;
      console.log(res.data);
    }
  },
  //在生命周期中就执行getMenuList方法
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  }
};
</script>
<style lang='less' scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.userImg {
  width: 58px;
  height: 60px;
  border-radius: 70px;
  img {
    width: 100%;
    border-radius: 70px;
  }
}
</style>
